package com.hiray.restcache.api

import android.content.res.Resources
import com.hiray.restcache.BuildConfig
import com.hiray.restcache.helper.Helper
import com.zmy.rxcache.rxjava2.RxJavaReplyCallFactory
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

class RestApiHelper {


    companion object {

        fun create(): RestApi {

            val okClient = OkHttpClient.Builder()
                .addInterceptor { chain -> //拦截请求，添加自定义的 Header 参数

                    val request = chain.request().newBuilder()
                        .addHeader("apptoken", Helper.token())
                        .addHeader("appdevice", "android")
                        .addHeader("plat", "official")
                        .addHeader("appversion", "202006121809")
                        .addHeader(
                            "device-density",
                            Resources.getSystem().displayMetrics.density.toString()
                        )
                        .build()
                    chain.proceed(request)
                }
                .build()
            return Retrofit.Builder()
                .client(okClient)
                .addCallAdapterFactory(RxJavaReplyCallFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BuildConfig.BASE_URL)
                .build()
                .create(RestApi::class.java)

        }
    }

}